﻿using MyHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using clf.weighing.entity;

namespace weighing
{
    public class ScaleDao : BaseDao<Scale>
    {
        #region instance

        private static ScaleDao? instance;
        public static ScaleDao Instance => instance ??= new ScaleDao();

        public override string GetTableName()
        {
            return TableName.scale.ToString();
        }
        #endregion


        /// <summary>
        /// 获取当前矿点的
        /// </summary>
        /// <returns></returns>
        public List<Scale> GetList()
        {
            String conditon = ScaleField.client_id.ToString() + $" = {App.InitConfig.clientId}";
            String sql = SqlBuilder.GetSelectSqlWithSoftDeleteCondition(GetTableName(), null, conditon);
            var data = Sqlite.Select<Scale>(sql);
            return data;
        }

        public bool IsExist(Scale scale)
        {
            String condition = ScaleField.com.ToString() + $" = '{scale.com}' or {ScaleField.name} = '{scale.name}' and {ScaleField.client_id} = '{App.InitConfig.clientId}'";
            String sql = SqlBuilder.GetSelectSqlWithSoftDeleteCondition(GetTableName(), "id", condition, null, null, null, 1, 0);
            var data = Sqlite.Select<Scale>(sql);
            return data.IsNotNullOrEmpty();
        }


        public int GetCount()
        {
            String sql = SqlBuilder.GetSelectSqlWithSoftDeleteCondition(GetTableName(), "id");
            var data = Sqlite.Select<Scale>(sql);
            return data.Count;
        }

        public int Delete(Scale? scale, Boolean isTrue = false)
        {
            if (scale == null)
            {
                return 0;
            }

            if (isTrue)
            {
                return Sqlite.Delete(scale, isTrue);
            }
            else
            {
                String condition = ScaleField.id.ToString() + $" = {scale.id}";
                String time = DateTimeHelper.getCurrentDateTime();
                String set = $" {ScaleField.is_delete} = 1 " + $", {ScaleField.delete_time} = '{time}'";
                String sql = SqlBuilder.GetUpdateSql(GetTableName(), set, condition);
                return Sqlite.Delete(sql);
            }
        }

    }
}
